package net.m2technologies.open_arm.utilities.generic_object_functions;

import java.lang.reflect.Field;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
class HashCodeWorker extends ReflectiveWorker {

    private int hashCodeRecursiveResult;

    public int hashCode(final Object objectA) {
        int result = 0;
        if (0 != this.hashCodeRecursiveResult) {
            this.hashCodeRecursiveResult = 0;
        }
        final Class clazz = objectA.getClass();
        try {
            //peel out and apart any arrays, including arrays of primitives...
            final Object[] tempArr = isObjectArray(objectA);
            if (null != tempArr) {
                for (int i = 0; i < tempArr.length; i++) {
                    result += getObjectValueAsString(objectA).hashCode();
                }
            } else {//not an array... Might contain arrays, however...
                result = handleObject(clazz, result, objectA);
            }
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        }
        final Class superClazz = clazz.getSuperclass();
        if (superClazz != clazz
            && !superClazz.equals(Object.class)) {
            result += hashCodeRecursive(objectA, superClazz);
        }
        return result;
    }

    private int handleObject(final Class clazz, final int result, final Object objectA) {
        int result1 = result;
        final Field[] fields = getAccessibleFields(clazz);
        for (int i = 0; i < fields.length; i++) {
            final Field field = fields[i];
            result1 += field.getName().hashCode();
            result1 += getFieldValueAsString(field, objectA).hashCode();
        }
        return result1;
    }

    private int hashCodeRecursive(final Object objectA,
                                  final Class clazzA) {
        try {
            this.hashCodeRecursiveResult = handleObject(clazzA, this.hashCodeRecursiveResult, objectA);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (StackOverflowError er) {
            return this.hashCodeRecursiveResult;
        }
        final Class superClazz = clazzA.getSuperclass();
        if (superClazz != clazzA && !superClazz.equals(Object.class)) {
            this.hashCodeRecursiveResult += hashCodeRecursive(objectA, superClazz);
        }
        return this.hashCodeRecursiveResult;
    }

}
